//
//  DSSHBaseScrollVC.m
//  DSSH
//
//  Created by Fuhao on 16/4/12.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "DSSHBaseScrollVC.h"
#import "DSSHLoginViewController.h"
#import "CusAlterview.h"
#import "DSSHDetailinfoVC.h"
#import "Cellofxib.h"

#define LINE 0.5
#define BUTTONWIDTH (([UIScreen mainScreen].bounds.size.width -(titlearray.count )*2)/titlearray.count-1)
#define TABLEWIDTH ([UIScreen mainScreen].bounds.size.width - 20)
@interface DSSHBaseScrollVC ()<UITableViewDelegate,UITableViewDataSource,Cellofxibdelegate,UIScrollViewDelegate,cusalterviewdelegate,UITextFieldDelegate,UIAlertViewDelegate>
{
    UIView *_modleview;
    UIView *_scview;
    NSArray *_dataarray;
    int     _index;
    UIButton *_blackgroundview ;
    NSMutableArray *_datamutarray;
    Cellofxib *_hitcell;
    CusAlterview *_showalterview;
    UIAlertView *_loginA;

    
    
}


@end

@implementation DSSHBaseScrollVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setbasicview];
    
    //self.view.frame = CGRectMake(0, 64,ZYAppWidth,ZYAppHeight);
    _dataviews = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 110,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 108)];
    
    [_dataviews setBackgroundColor:[UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1.0]];
    _dataviews.pagingEnabled = YES;
    _dataviews.delegate = self;
    _dataviews.contentSize = CGSizeMake(ZYAppWidth * 3, 0);
    [self.view addSubview:_dataviews];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(!self.navigationItem.titleView )
    {
       self.navigationItem.titleView = [self createseachview]; 
    }
    static dispatch_once_t onceToken;
    
    [self viewtablevews];
}
-(void)viewtablevews
{
    //    _datamutarray = [NSMutableArray array];
    for(int i = 0; i <_dataarray.count ;i++){
       if([_dataviews viewWithTag:[[NSString stringWithFormat:@"1000%d",i] intValue ]] )
       {
           continue;
       }
        UITableView *tab = [[UITableView alloc]init];
        tab.delegate = self;
        tab.dataSource = self;
        tab.layer.cornerRadius =5.0;
        tab.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1.0];
        tab.tag = i+10000;
        tab.tableFooterView = [[UIView alloc] init];
        
        //        tab.frame = CGRectMake(10*(i+1) + i*(TABLEWIDTH +10), 2, TABLEWIDTH, 44*_dataoftable.count);
        
        [_dataviews addSubview:tab];
        [tab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_dataviews.mas_top).with.offset(2);
            make.left.equalTo(_dataviews.mas_left).with.offset(10*(i+1) + i*(TABLEWIDTH +10)  );
            make.width.mas_equalTo(TABLEWIDTH);
            if(44*((NSArray *)_dataoftable[i]).count > ZYAppHeight - 44 - 49 -64)
            {
                make.height.mas_equalTo(ZYAppHeight - 44 - 49 -64); //设置tbleview的高度
            }
            else  make.height.mas_equalTo(44*((NSArray *)_dataoftable[i]).count);
                
            
            }];
        [_datamutarray addObject:tab];
    }
    
    self.tableView0 = [_dataviews viewWithTag:10000];
    self.tableView1 = [_dataviews viewWithTag:10001];
    self.tableView2 = [_dataviews viewWithTag:10002];
    
    

    
}

- (NSMutableArray *)dataoftable{
    if (_dataoftable == nil) {
        _dataoftable = [NSMutableArray array];
    }
    return _dataoftable;
}

-(void)setbasicview
{
    UIView *modeview1= [[UIView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    modeview1.backgroundColor = [UIColor whiteColor];
    NSArray *titlearray = @[@"上刊模式",@"巡查模式",@"检修模式"];
    _dataarray = titlearray;
    for (int i = 0; i < titlearray.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        [button setTitle:titlearray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(selectModle:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 10+i;
        [modeview1 addSubview:button];
        button.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(BUTTONWIDTH);
            make.left.equalTo(modeview1.mas_left).with.offset(i*BUTTONWIDTH+(i+1)*LINE);
            make.top.equalTo(modeview1.mas_top).with.offset(0);
            make.height.equalTo(@44);
            
        }];
        if(i == _dataarray.count -1){
            [self selectModle:button];
        }
        
        
    }
    for (int i = 0; i < titlearray.count -1; i++) {
        UIView *blackview = [[UIView alloc]init];
        blackview.backgroundColor = [UIColor grayColor];
        [modeview1 addSubview:blackview];
        [blackview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(modeview1.mas_left).with.offset((BUTTONWIDTH+LINE)*(i+1));
            make.centerY.equalTo(modeview1.mas_centerY);
            make.width.mas_equalTo(LINE);
            make.height.equalTo(@38);
        }];
        
    }
    //添加滑动条
    UIView *scview = [[UIView alloc]init];
    scview.backgroundColor = [UIColor redColor];
    [modeview1 addSubview:scview];
    [scview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@3);
        make.bottom.equalTo(modeview1.mas_bottom).with.offset(0);
        make.centerX.equalTo([modeview1 viewWithTag:10]);
        make.width.mas_equalTo(BUTTONWIDTH - 20);
    }];
    _scview = scview;
    _modleview = modeview1;
    
    [self.view addSubview:_modleview];
    _index = 0;
    
}

#pragma mark - 创建视图
-(UIView *)createseachview
{
    if(!_isSearchhid)
    {
        UIView *Mview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width-100,30)];
        Mview.backgroundColor = [UIColor whiteColor];
        Mview.layer.cornerRadius = 5;
        
        UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fangdajing"]];
        [Mview addSubview:imageview];
        [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(@20);
            make.centerY.equalTo(Mview.mas_centerY);
            make.left.equalTo(Mview.mas_left).with.offset(5);
        }];
        UITextField *textfield = [[UITextField alloc] init];
        [Mview addSubview:textfield];
        textfield.delegate = self;
        textfield.font = [UIFont systemFontOfSize:13.0];
        textfield.placeholder = @"搜索相关内容";
        [textfield mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageview.mas_right).with.offset(0);
            make.centerY.equalTo(Mview.mas_centerY);
            make.height.equalTo(@30);
            make.right.equalTo(Mview.mas_right).with.offset(0);
        }];
        _searchT = textfield;
        return Mview;
    }
    else
        return nil;
    
    
}


#pragma mark - 事件点击按钮
-(void)selectModle:(UIButton *)button
{
    DSSHLog(@"modle");
    
    //移动滑动条
    [UIView animateWithDuration:0.5 animations:^{
        CGPoint center = CGPointMake(button.center.x, _scview.center.y);
        _scview.center =center;
    }];
    _index = (int)button.tag - 10 ;
    [UIView animateWithDuration:0.5 animations:^{
        _dataviews.contentOffset= CGPointMake((self.view.bounds.size.width ) * _index  , 0);
    }];
    
    DSSHLog(@"%@",NSStringFromCGPoint(_dataviews.contentOffset));
}

-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == _tableView0)
    {
        return ((NSMutableArray *)_dataoftable[0]).count;
    }
    else if(tableView == _tableView1)
    {
        return ((NSMutableArray *)_dataoftable[1]).count;
    }
    else if(tableView == _tableView2)
    {
        return ((NSMutableArray *)_dataoftable[2]).count;
    }
    else return 0;
    
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    celldata *datac = nil;
    Cellofxib *cell = [tableView dequeueReusableCellWithIdentifier:@"DATA"];
    if(cell == nil){
        cell = [Cellofxib initWithxib:@"Updatacell"];
        if(self.isSearchhid)
        {
            [cell.upbutton setHidden:YES];
            [cell.weakbutton setHidden:NO];
        }
        datac = [[celldata alloc] init];
    }
    cell.delegate = self;
    //设置每个cell的数据
    if(tableView == _tableView0){
        
       
        datac.maintitle = _dataoftable[0][indexPath.row];
        cell.info = datac;
        
    }
    if(tableView == _tableView1){
        
       
        datac.maintitle = _dataoftable[1][indexPath.row];
        cell.info = datac;
    }
    if(tableView == _tableView2){
        
        
        datac.maintitle = _dataoftable[2][indexPath.row];
        cell.info = datac;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark cell's delegate
-(void)upbuttonofcell:(Cellofxib *)cell Netsta:(BOOL)sta
{
    if(sta)
    {
        cell.info.datasta = Databeup;
    }
    else
        [self showcusAlertV:cell];
    
}
-(void)showcusAlertV:(Cellofxib *)cell
{
    CusAlterview *alterv = [CusAlterview  initWithxib];
    alterv.frame = CGRectMake(0, 0, 0, 0);
    alterv.delegate =self;
    _hitcell = cell;
    alterv.center = self.view.center;
    _blackgroundview = [[UIButton alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
    _blackgroundview.backgroundColor = [UIColor grayColor];
    _blackgroundview.alpha = 0.7;
    [_blackgroundview setUserInteractionEnabled:YES];
    alterv.blackbutton = _blackgroundview;
    
    [[UIApplication sharedApplication].keyWindow addSubview:_blackgroundview];
    [[UIApplication sharedApplication].keyWindow addSubview:alterv];
    [alterv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(300);
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
    }];
    _showalterview = alterv;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //     scrollView的contentOffset是最重要的属性,点,x,y记录的是滚动的距离,相对的是scrollView的可视界面的左上角的距离
    
    NSInteger page = _dataviews.contentOffset.x / ZYAppWidth ;
    
    [self viewsrcoll:[_modleview viewWithTag:page + 10]];
    
    _index = (int)page;
    
}


-(void)viewsrcoll:(UIButton *)button{
    //移动滑动条
    [UIView animateWithDuration:0.25 animations:^{
        CGPoint center = CGPointMake(button.center.x, _scview.center.y);
        _scview.center =center;
    }];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(void)Okbuttonalterview:(CusAlterview *)cusa{
    if(cusa.buttonsta)
    {
        [_hitcell.timesla setHidden:NO];
        [_hitcell.upbutton setHidden:YES];
        [_hitcell.reupbutton setHidden:NO];
        _hitcell.info.datasta = Databeup;
    }
    [_showalterview removeFromSuperview];
    [_blackgroundview removeFromSuperview];
    
    
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
//- (void)textFieldBeginEditing:(UITextField *)textField
//{
//    DSSHLog(@"开始编辑");
//    [self toLoginAlter];
//}
//提示登录
-(void)toLoginAlter
{
    _loginA = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未登录，是否登录 ?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去登录", nil];
    _loginA.delegate =self;
    [_loginA show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
    {
        UIStoryboard *story =[UIStoryboard storyboardWithName:@"DSSHLogin" bundle:nil];
        DSSHLoginViewController *login = [story instantiateInitialViewController];
        [self.navigationController presentViewController:login animated:YES completion:^{
            [_loginA  dismissWithClickedButtonIndex:0 animated:NO];
        }];
    }
}
@end
